<template>
	<view>
		<uni-nav-bar dark :title="startTime" fixed :statusBar="true" left-icon="left" @clickLeft="onLeft">
			<block slot="right">
				<view class="city">
					<view @click="onEnd">
						结束
					</view>
				</view>
			</block>

		</uni-nav-bar>
		<view class="header">
			<view @click="onStop(info.oneTeam.id, 1)">{{info.oneTeam.teamName}}</view>
			<!-- <text>{{fenData.length ? fenData[0].teamScore: 0}}:{{fenData.length ? fenData[1].teamScore : 0}}</text> -->
			<text>{{oneSource}}:{{twoSource}}</text>
			<view @click="onStop(info.twoTeam.id, 2)">{{info.twoTeam.teamName}}</view>
		</view>
		<view class="headerTwo">
			<view class="oneItem">
				<text>暂停
					<span v-if="subsection <=2">{{(zanTingOne[1] || 0) + (zanTingOne[2] || 0)}}</span>
					<span v-else>{{zanTingOne[subsection <= 2 ? subsection : 3 ] || 0}}</span>
				</text>
				<text>犯规 <span style="color: red;"> {{fanGuiNumOne[subsection <= 3 ? subsection : 4] || 0}}</span></text>
			</view>
			<view class="cent">
				<uni-icons type="arrow-left" @click="onAddSub(0)" color="#fff" size="30"></uni-icons>
				第{{subsection}}节
				<uni-icons type="arrow-right" @click="onAddSub(1)" color="#fff" size="30"></uni-icons>
			</view>
			<view class="twoItem">
				<text>暂停
					<span v-if="subsection <=2">{{(zanTingTwo[1] || 0) + (zanTingTwo[2] || 0)}}</span>
					<span v-else>{{zanTingTwo[subsection <= 2 ? subsection : 3 ] || 0}}</span>
				</text>
				<text>犯规 <span style="color: red;"> {{fanGuiNumTwo[subsection <= 3 ? subsection : 4] || 0}}</span></text>
			</view>
		</view>
		<view class="content">
			<view class="line1"></view>
			<view class="line2"></view>
			<view class="line3"></view>
			<view class="ti1">替补球员</view>
			<view class="ti2">替补球员</view>

			<view class="add-1" @click="addTeamUser(info.oneTeam.id)">
				<uni-icons type="plus" size="120rpx"></uni-icons>
			</view>
			<view class="add-2" @click="addTeamUser(info.twoTeam.id)">
				<uni-icons type="plus" size="120rpx"></uni-icons>
			</view>
			<sortableRenderjs @onStart="onTouchStart()" class="contentItem" :style="{height: `calc(140rpx + ${postList[postList.length - 1].y})`}" :options="{group: 'shared'}" v-if="isHow" domId="horizontal"
				@changeList="(data) => horizontalListChange(data, 1)" ref="sortableRenderjs">
				<view id="horizontal" class="horizontal item-ul" >

					<view class="item" v-for="(item, index) in list"  @touchend="onSelect(item, 1, item, index)"
						:class="{cur: selectUserId == item.id}"
						:style="{'left': postList[index] ? postList[index].x : 0, top: postList[index] ? postList[index].y || 0 : 0, background: teamOneColour}"
						:key="item.id" :data-id="JSON.stringify(item)">

						<view>
							<text class="num"
								v-if="fenData[0][item.basketballPlayerId]">{{fenData.length ?  fenData[0][item.basketballPlayerId] : 0}}</text>
							{{item.playerNumber}}
						</view>
						<text class="name">
							{{item.playerName}}
						</text>

					</view>
				</view>

			</sortableRenderjs>
			<sortableRenderjs  @onStart="onTouchStart()" class="contentItem" v-if="isHow" domId="horizontal1"
				@changeList="(data) => horizontalListChange(data, 2)" ref="sortableRenderjs">
				<view id="horizontal1" class="horizontal item-ul">
					<view class="item" v-for="(item, index) in list1" @touchend="onSelect(item, 2,item, index)"
						:class="{cur: selectUserId == item.id}"
						:style="{'left': postList[index] ? postList[index].x : 0, top: postList[index] ? postList[index].y || 0 : 0, background: teamTwoColour}"
						:key="item.id" :data-id="JSON.stringify(item)">
						<view>
							<text class="num"
								v-if="fenData[1][item.basketballPlayerId]">{{fenData.length ?  fenData[1][item.basketballPlayerId] : 0}}</text>
							{{item.playerNumber}}
						</view>
						<text class="name">
							{{item.playerName}}
						</text>
					</view>
				</view>

			</sortableRenderjs>
		</view>

		<view class="btns">
			<button @click="onrealtime">实时数据</button>
			<!-- <button @click="onSwitchPlayer">换人</button> -->
			<button @click="allFun">全部事件</button>
			<button @click="onplayback">即时回放</button>
		</view>
		<uni-popup ref="popup" background-color="#fff" @maskClick="onSelectUserClose">
			<view class="downboxList" v-if="downbox.length">
				<view v-if="scoringWay == 0">
					<view class="downbox">
						<view  @click="onselectPlayerFen({code: 0})"
							:style="{background: '#f6c605'}">
							不中
						</view>
						<view  @click="onselectPlayerFen({code: 9})"
							:style="{background: '#547b44'}">
							篮板
						</view>
					</view>
					<view class="downbox">
						<view  @click="onselectPlayerFen({code: 1})"
							:style="{background: '#547b44'}">
							二分命中
						</view>
						<view  @click="onselectPlayerFen({code: 2})"
							:style="{background: '#547b44'}">
							三分命中
						</view>
						<view  @click="onselectPlayerFen({code: 15})"
							:style="{background: '#547b44'}">
							罚球命中
						</view>
						
						<view  @click="onselectPlayerFen({code: 4})"
							:style="{background: '#547b44'}">
							抢断
						</view>
						<view  @click="onselectPlayerFen({code: 3})"
							:style="{background: '#547b44'}">
							盖帽
						</view>
						<view  @click="onselectPlayerFen({code: 8})"
							:style="{background: '#547b44'}">
							助攻
						</view>
					</view>
					<view class="downbox">
						
						<view  @click="onselectPlayerFen({code: 10})"
							:style="{background: 'red'}">
							失误
						</view>
						<view  @click="onselectPlayerFen({code: 11})"
							:style="{background: 'red'}">
							犯规
						</view>
						<view  @click="onselectPlayerFen({code: 19})"
							:style="{background: 'red'}">
							团队犯规
						</view>
					</view>
				</view>
				<view v-if="scoringWay == 1">
					<view class="downbox">
						<view  @click="onselectPlayerFen({code: 0})"
							:style="{background: '#f6c605'}">
							不中
						</view>
						<view  @click="onselectPlayerFen({code: 9})"
							:style="{background: '#547b44'}">
							篮板
						</view>
					</view>
					<view class="downbox">
						<view  @click="onselectPlayerFen({code: 14})"
							:style="{background: '#547b44'}">
							一分命中
						</view>
						<view  @click="onselectPlayerFen({code: 1})"
							:style="{background: '#547b44'}">
							二分命中
						</view>
						<view  @click="onselectPlayerFen({code: 15})"
							:style="{background: '#547b44'}">
							罚球命中
						</view>
						<view  @click="onselectPlayerFen({code: 8})"
							:style="{background: '#547b44'}">
							助攻
						</view>
						<view  @click="onselectPlayerFen({code: 3})"
							:style="{background: '#547b44'}">
							盖帽
						</view>
						<view  @click="onselectPlayerFen({code: 4})"
							:style="{background: '#547b44'}">
							抢断
						</view>
					</view>
					<view class="downbox">
						
						<view  @click="onselectPlayerFen({code: 10})"
							:style="{background: 'red'}">
							失误
						</view>
						<view  @click="onselectPlayerFen({code: 11})"
							:style="{background: 'red'}">
							犯规
						</view>
						<view  @click="onselectPlayerFen({code: 19})"
							:style="{background: 'red'}">
							团队犯规
						</view>
					</view>
				</view>
			</view>

		</uni-popup>

		<uni-popup ref="popupOther" background-color="#fff">
			<view class="downbox" v-if="scoringWay == 1">
				<view @click="onselectPlayerFen({code: 16})" style="background-color: #f6c605;">
					一分不中
				</view>
				<view @click="onselectPlayerFen({code: 5})" style="background-color: #f6c605;">
					二分不中
				</view>
				<view @click="onselectPlayerFen({code: 7})" style="background-color: #f6c605;">
					罚球不中
				</view>
			</view>
			<view class="downbox" v-if="scoringWay == 0">
				<view @click="onselectPlayerFen({code: 5})" style="background-color: #f6c605;">
					二分不中
				</view>
				<view @click="onselectPlayerFen({code: 6})" style="background-color: #f6c605;">
					三分不中
				</view>
				<view @click="onselectPlayerFen({code: 7})" style="background-color: #f6c605;">
					罚球不中
				</view>
			</view>
		</uni-popup>

		<uni-popup ref="addTeamUser" background-color="#fff" type="dialog">

			<uni-popup-dialog before-close cancelText="取消" confirmText="确定" title="添加球员" content=""
				@confirm="dialogConfirm" @close="onCloseAdd">
				<view>
					<uni-easyinput v-model="addUserData.name" placeholder="请输入球员姓名"></uni-easyinput>
					<view style="margin-top: 20rpx;">
						<uni-easyinput v-model="addUserData.playerNumber" placeholder="请输入球号"></uni-easyinput>
					</view>
				</view>
				
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import sortableRenderjs from '@/uni_modules/cms-sortable-renderjs/components/cms-sortable-renderjs/cms-sortable-renderjs.vue'
	import {
		getdownBox,
		getfen,
		getplayerLog,
		putplayerPositionLog,
		getcreatePosition,
		postsubstitution,
		getplayerPositionLogList,
		overTournament
	} from '@/api/record/index.js'
	import {addbasketballPlayer,
	substitutionPlayer,
	setSaveBatch,
	getlistAll,
	switchGameSections} from '@/api/basketballTeam/index.js'
	export default {
		components: {
			sortableRenderjs
		},
		data() {
			return {
				oneSource: 0,
				twoSource: 0,
				postList: [{
						x: '30rpx',
						y: ''
					},
					{
						x: '240rpx',
						y: ''
					},
					{
						x: '130rpx',
						y: '100rpx'
					},
					{
						x: '30rpx',
						y: '180rpx'
					},
					{
						x: '240rpx',
						y: '180rpx'
					},
					{
						x: '30rpx',
						y: '340rpx'
					},
					{
						x: '130rpx',
						y: '340rpx'
					},
					{
						x: '30rpx',
						y: '550rpx'
					},
					

				],
				list: [],
				list1: [],
				isHow: true,
				teamOneColour: '',
				teamTwoColour: '',
				downbox: [],
				basketballTeamId: '',
				basketballPlayerId: '',
				info: {},
				fenData: [{}, {}],
				subsection: 1,
				id: '',
				interTime: 0,
				newTime: 0,
				startTime: '',
				otherdownbox: [],
				selectUserId: 0,
				scoringWay: '',
				stopId: '',
				addUserData: {
					name: '',
					playerNumber: '',
					playerId: ''
				},
				loaclUserInfo: 	{}, // 本地存储的使用user信息
				localId: 0,
				fanGuiNumTwo: {},
				fanGuiNumOne: {},
				zanTingOne: {},
				zanTingTwo: {},
				timeServer: null,
				selectpostList: {},
				selectIUserIndex: 0,
				isOpenSelect: true
			}

		},
		methods: {
			onTouchStart() {
				console.log('-----------------start')
				this.isOpenSelect = false;
				this.selectUserId = ''
				this.$refs.popup.close();
				this.$refs.popupOther.close(); 
			},
			onplayback() {
				uni.getNetworkType({
					success: (res) => {
						if (res.networkType == 'none' || res.networkType == '3g') {
							this.$modal.msg('当前网络不佳无法查看')
						} else {
							clearTimeout(this.timeServer);
							uni.showLoading({
								title: '正在上传网络数据...'
							})
							this._setSaveBatch(true, () => {
								
								uni.navigateTo({
									url: '/pages/record/playback?id=' + this.id
								})
							})
							
						}
					}
				});
				
			},
			allFun(){
				uni.getNetworkType({
					success: (res) => {
						console.log(res.networkType);
						if (res.networkType == 'none' || res.networkType == '3g') {
							this.$modal.msg('当前网络不佳无法查看')
						} else {
							clearTimeout(this.timeServer);
							uni.showLoading({
								title: '正在上传网络数据...'
							})
							this._setSaveBatch(true, () => {
								
								uni.navigateTo({
									url: '/pages/record/sportsData?tournamentId=' + this.id
								})
							})
							
						}
					}
				});
				// uni.navigateTo({
				// 	url: '/pages/record/sportsData?tournamentId=' + this.id
				// })
			},
			onSwitchPlayer() {
				uni.getNetworkType({
					success: (res) => {
						if (res.networkType == 'none' || res.networkType == '3g') {
							this.$modal.msg('当前网络不佳无法查看')
						} else {
							substitutionPlayer({tournamentId: this.id}).then(res => {
								console.log('substitutionPlayer---')
								let data = []
								let x = 30
								let y = 580
								let dataist = res.oneTeamList.length > res.twoTeamList.length ? res.oneTeamList : res.twoTeamList
								dataist.forEach((item, index) => {
									// console.log(index);
									if (index > 7) {
										
										this.postList.push({
											x: x + 'rpx',
											y: y + 'rpx'
										});
										(x += 110)
										if (index % 3 == 0) {
											x = 30
											y += 150
										}
									}
								})
								this.list = res.oneTeamList;
								this.list1 = res.twoTeamList
								
								
								this.$nextTick(() => {
									this.isHow = true
								})
							})
						}
					}
				});
				
			},
			onCloseAdd() {
				this.addUserData = {
					name: '',
					playerNumber: '',
					playerId: ''
				}
				this.$refs.addTeamUser.close();
			},
			dialogConfirm() {
				let {name, playerNumber} = this.addUserData
				if(!name) {
					this.$modal.msg('请输入球员姓名')
					return;
				}
				if(!playerNumber) {
					this.$modal.msg('请输入球号')
					return;
				}
				addbasketballPlayer('post', {...this.addUserData, tournamentId: this.id,}).then(res => {
					this.list = [];
					this.list1 = []
					this.isHow = true
					this.getCteateList()
					this.$refs.addTeamUser.close();
				})
				
			},
			addTeamUser(id) {
				this.$refs.addTeamUser.open('center')
				this.addUserData.playerId = id
			},
			onStop(id, isTeam) {
				// alert(isTeam);
				this.stopId = id
				this.selectIUserIndex = 0
				uni.showModal({
					content: this.interTime ? '确定暂停吗？' : '确定恢复比赛吗？',
					success: (res) => {
						if (res.confirm) {
							console.log('zanting----')
							let contestId = uni.getStorageSync('contestId')
							let info = uni.getStorageSync('info')
							this.localId = uni.getStorageSync('localId') || 0;
							this.loaclUserInfo = {
								basketballTeamId: id,
								contestId,
								type: this.interTime ? 13 : 17,
								tournamentId: info.id,
								subsection: this.subsection,
								time: this.startTime + '',
								id: this.id + (this.localId += 1),
								isTeam
							}
							uni.setStorageSync('localId', this.localId);
							this.onselectPlayerFen({code:this.interTime ? 13 : 17},isTeam)
							if (this.interTime) {
								this.getFenNum()
								clearInterval(this.interTime)
								this.interTime = ''
							} else {
								this.createTime();
							}
							return;
							getplayerLog({
								// basketballPlayerId: this.basketballPlayerId,
								basketballTeamId: id,
								contestId,
								type: this.interTime ? 13 : 17,
								tournamentId: info.id,
								subsection: this.subsection,
								time: this.startTime + ''
							}).then(res => {
								if (this.interTime) {
									
								} else {
									this.createTime();
								}

							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			onSelectUserClose() {
				this.selectUserId = 0
			},
			onEnd() {
				uni.getNetworkType({
					success: (res) => {
						if (res.networkType == 'none' || res.networkType == '3g') {
							this.$modal.msg('请先连接网络后在结束比赛')
						} else {
							uni.showLoading({
								title: '正在上传网络数据...'
							})
							this._setSaveBatch(true, () => {
								uni.navigateTo({
									url: '/pages/record/realtime?id=' + this.id
								})
							})
						}
					}
				});
			},
			// 获取必反
			getFenNum() {
				let info = uni.getStorageSync('info')
				this.info = info;
				let subsection = uni.getStorageSync('subsection')
				this.subsection = subsection
				getfen({
					tournamentId: info.id,
					oneTeamId: info.oneTeam.id,
					twoTeamId: info.twoTeam.id,
					subsection: this.subsection
				}).then(res => {
					//this.fenData = res.data
					this.fenData = [res.data[0].playerNum, res.data[1].playerNum]
					uni.setStorageSync('fenData', [res.data[0].playerNum, res.data[1].playerNum])
				})
			},
			onLeft() {
				uni.getNetworkType({
					success: (res) => {
						if (res.networkType == 'none' || res.networkType == '3g') {
							this.$modal.msg('当前网络不佳无法返回')
						} else {
							clearInterval(this.interTime);
							clearTimeout(this.timeServer);
							uni.showLoading({
								title: '正在上传网络数据...'
							})
							this._setSaveBatch(true, () => {
								
								uni.navigateBack()
							}).catch(err => {
								
							});
							
						}
					}
				});
				
			},
			onAddSub(is) {
				if (is) {
					this.subsection += 1
					clearInterval(this.interTime);
					this.newTime = 0
					this.createTime()
				} else {
					if (this.subsection != 1) {
						this.subsection -= 1
					}

				}
				uni.setStorageSync('subsection', this.subsection);
				switchGameSections({id: this.id, subsection:this.subsection})
				this.getFenNum(this.subsection);
				// this.onselectPlayerFen(this.subsection)
			},
			setSource(isTeam, item) {
				// 保存总比分
				let sourceOne = uni.getStorageSync('sourceOne') || 0;
				let sourceTwo = uni.getStorageSync('sourceTwo') || 0;
				if (isTeam == 1) {
					if (item.code == 1) {
						sourceOne += 2
					} 
					if (item.code == 2) {
						sourceOne += 3
					}
					if (item.code == 15) {
						sourceOne += 1
					}
					if (item.code == 14) {
						sourceOne += 1
					}
					uni.setStorageSync('sourceOne', sourceOne)
				} else {
					if (item.code == 1) {
						sourceTwo += 2
					}
					if (item.code == 2) {
						sourceTwo += 3
					}
					if (item.code == 15) {
						sourceTwo += 1
					}
					if (item.code == 14) {
						sourceTwo += 1
					}
					uni.setStorageSync('sourceTwo', sourceTwo)
				}
				// 重新负值比分
				this.oneSource = sourceOne;
				this.twoSource = sourceTwo;
			},
			setSourceList() {
				if (!uni.getStorageSync('sourceList')) {
					uni.setStorageSync('sourceList', [this.loaclUserInfo])
				} else {
					let arr = uni.getStorageSync('sourceList')
					arr.push(this.loaclUserInfo)
					uni.setStorageSync('sourceList', arr);
				}
			},
			setFanGui(isTeam, item, info) {
				let fanGuiNumOne = uni.getStorageSync('fanGuiNumOne') || {};
				let fanGuiNumTwo = uni.getStorageSync('fanGuiNumTwo') || {};
				if (isTeam == 1) {
					if (info.subsection <= 3) {
						fanGuiNumOne[info.subsection] = fanGuiNumOne[info.subsection] ? fanGuiNumOne[info.subsection] += 1 : 1;
					} else {
						fanGuiNumOne[4] = fanGuiNumOne[4] ? fanGuiNumOne[4] += 1 : 1;
					}
				} else {
					if (info.subsection <= 3) {
						fanGuiNumTwo[info.subsection] = fanGuiNumTwo[info.subsection] ? fanGuiNumTwo[info.subsection] += 1 : 1;
					} else {
						fanGuiNumTwo[4] = fanGuiNumTwo[4] ? fanGuiNumTwo[4] += 1 : 1;
					}
				}
				uni.setStorageSync('fanGuiNumOne', fanGuiNumOne);
				uni.setStorageSync('fanGuiNumTwo', fanGuiNumTwo);
				this.fanGuiNumOne = fanGuiNumOne;
				this.fanGuiNumTwo = fanGuiNumTwo;
			},
			tuanFanGui() {
				
			},
			setZanTing(isTeam, item, info) {
				// debugger
				let zanTingOne = uni.getStorageSync('zanTingOne') || {};
				let zanTingTwo = uni.getStorageSync('zanTingTwo') || {};
				if (isTeam == 1) {
					if (info.subsection <= 3) {
						zanTingOne[info.subsection] = zanTingOne[info.subsection] ? zanTingOne[info.subsection] += 1 : 1;
					} else {
						zanTingOne[3] = zanTingOne[3] ? zanTingOne[3] += 1 : 1;
					}
				} else {
					if (info.subsection <= 3) {
						zanTingTwo[info.subsection] = zanTingTwo[info.subsection] ? zanTingTwo[info.subsection] += 1 : 1;
					} else {
						zanTingTwo[3] = zanTingTwo[3] ? zanTingTwo[3] += 1 : 1;
					}
				}
				uni.setStorageSync('zanTingOne', zanTingOne);
				uni.setStorageSync('zanTingTwo', zanTingTwo);
				this.zanTingOne = zanTingOne;
				this.zanTingTwo = zanTingTwo;
			},
			onselectPlayerFen(item) {
				console.log('zanting---------11')
				
				this.loaclUserInfo.time = this.startTime;
				this.loaclUserInfo.createTime = new Date().getTime();
				this.loaclUserInfo.type = item.code;
				if (item.code == 0) {
					this.$refs.popupOther.open('bottom')
					return;
				}
				// 记录比分
				this.setSource(this.loaclUserInfo.isTeam, item);
				// 保存球员得分记录
				this.setSourceList();
				// 记录犯规次数
				if (item.code == 11) {
					this.setFanGui(this.loaclUserInfo.isTeam, item, this.loaclUserInfo);
					// 记录犯规次数
					let fenData = uni.getStorageSync('fenData') || [{}, {}];
					let {basketballPlayerId} = this.loaclUserInfo;
					if (this.loaclUserInfo.isTeam == 1) {
						fenData[0][basketballPlayerId] = fenData[0][basketballPlayerId] ? fenData[0][basketballPlayerId]+=1 : 1
					} else {
						fenData[1][basketballPlayerId] = fenData[1][basketballPlayerId] ? fenData[1][basketballPlayerId]+=1 : 1
					}
					uni.setStorageSync('fenData',fenData);
					this.fenData = fenData;
				}
				// 记录团队犯规
				if (item.code == 19) {
					this.setFanGui(this.loaclUserInfo.isTeam, item, this.loaclUserInfo);
				}
				// 暂停
				if (item.code == 13) {
					this.setZanTing(this.loaclUserInfo.isTeam, item, this.loaclUserInfo)
				}
				this.$refs.popup.close();
				this.$refs.popupOther.close();
				this.selectUserId = 0
				
				// 更换球员位置
				if (this.selectIUserIndex<=5) {
					return;
				}
				
				uni.getNetworkType({
					success: (res) => {
						if (res.networkType == 'none' || res.networkType == '3g') {
							// this.$modal.msg('当前网络不佳无法查看')
						} else {
							if (this.loaclUserInfo.isTeam == 1) {
								
								let oldData = this.list[5];
								
								this.list[5] = this.selectpostList;
								this.list[this.selectIUserIndex] = oldData
								this.isHow = false;
								this.$nextTick(() => {
									this.isHow = true
									this.list = this.list;
									let playerOneId = this.list[5].basketballPlayerId
									let playerTwoId = this.list[this.selectIUserIndex].basketballPlayerId;
									let teamId = this.list[5].basketballTeamId
									postsubstitution({
										playerOneId,
										playerTwoId,
										tournamentId: this.id,
										teamId
									})
								});
								
							}  else {
								let oldData = this.list1[5];
								this.list1[5] = this.selectpostList;
								this.list1[this.selectIUserIndex] = oldData
								this.isHow = false;
								this.$nextTick(() => {
									this.isHow = true
									this.list1 = this.list1;
									let playerOneId = this.list1[5].basketballPlayerId
									let playerTwoId = this.list1[this.selectIUserIndex].basketballPlayerId;
									let teamId = this.list1[5].basketballTeamId
									postsubstitution({
										playerOneId,
										playerTwoId,
										tournamentId: this.id,
										teamId
									})
								})
							}
						}
					}
				});
				
				
				
				
				
				return;
				let contestId = uni.getStorageSync('contestId')
				let info = uni.getStorageSync('info')
				let subsection = uni.getStorageSync('subsection')
				getplayerLog({
					basketballPlayerId: this.basketballPlayerId,
					basketballTeamId: this.basketballTeamId,
					contestId,
					type: item.code,
					tournamentId: info.id,
					subsection: subsection,
					time: this.startTime + ''
				}).then(res => {
					this.$refs.popup.close();
					this.$refs.popupOther.close();
					this.$modal.msg(res.msg)
					this.getFenNum(subsection);
					this.selectUserId = 0
				})
			},
			onSelect(item, num, selectpostList, userIndex) {
				if (!this.isOpenSelect) { 
					return;
				}
				console.log('---sekect--1')
				this.selectpostList = selectpostList;
				this.selectIUserIndex = userIndex
				let {basketballPlayerId, basketballTeamId} = item;
				this.localId = uni.getStorageSync('localId') || 0;
				this.loaclUserInfo = {
					basketballPlayerId,
					basketballTeamId,
					subsection: this.subsection,
					tournamentId: this.id,
					id: this.id + (this.localId += 1),
					isTeam: num
				};
				uni.setStorageSync('localId', this.localId);
				this.selectUserId = item.id
				let info = uni.getStorageSync('info')
				this.basketballTeamId = item.basketballTeamId;
				this.basketballPlayerId = item.basketballPlayerId
				this.$refs.popup.open('bottom')
			},

			horizontalListChange(list, is) {
				console.log('postsubstitution------1')
				
				let {
					oldIndex,
					newIndex,
					listData
				} = list
				let data;
				if (oldIndex != newIndex) {
					let teamId;
					if (is == 1) {
						console.log(this.list, '---');
						teamId = this.list[0].basketballTeamId
						data = JSON.parse(JSON.stringify(this.list))
					} else {
						data = JSON.parse(JSON.stringify(this.list1))
						teamId = this.list1[1].basketballTeamId
					}
					var itemToMove = data[oldIndex]; // 从原位置移除被拖拽的项
					var orderIem = data[newIndex];

					let playerOneId = itemToMove.basketballPlayerId
					let playerTwoId = orderIem.basketballPlayerId

					console.log(teamId, '-------1')



					postsubstitution({
						playerOneId,
						playerTwoId,
						tournamentId: this.id,
						teamId
					})


					data[oldIndex] = orderIem;

					data[newIndex] = itemToMove;
					this.isHow = false
					console.log(data)
					this.$nextTick(() => {
						this.isHow = true
						if (is == 1) {
							this.list = data
						} else {
							this.list1 = data
						}

					})
				}
				this.isOpenSelect = true;

			},
			formatSecond(result) {
				const h = Math.floor((result / 3600) % 24);
				const m = Math.floor((result / 60) % 60);
				const s = Math.floor(result % 60);
				result = (h >= 10 ? h : '0' + h) + ":" + (m >= 10 ? m : '0' + m) + ':' + (s >= 10 ? s : '0' + s);
				return result;
			},
			getCteateList() {
				getcreatePosition({
					tournamentId: this.id
				}).then(res => {
					console.log(res.oneTeamList, '----------');
					let data = []
					let x = 30
					let y = 690
					let dataList = res.oneTeamList.length > res.twoTeamList.length ? res.oneTeamList : res
						.twoTeamList
					dataList.forEach((item, index) => {
						// console.log(index);

						if (index > 6) {
							console.log(index);


							this.postList.push({
								x: x + 'rpx',
								y: y + 'rpx'
							});
							(x += 110)
							if (index % 3 == 0) {
								x = 30
								y += 150
							}
						}
					})
					console.log(this.postList, 'this.postList')
					this.list = res.oneTeamList;
					this.list1 = res.twoTeamList


					this.$nextTick(() => {
						this.isHow = true
					})

				})
			},
			onrealtime() {
				// clearInterval(this.interTime)
				uni.getNetworkType({
					success: (res) => {
						console.log(res.networkType);
						if (res.networkType == 'none' || res.networkType == '3g') {
							this.$modal.msg('当前网络不佳无法查看')
						} else {
							clearTimeout(this.timeServer);
							uni.showLoading({
								title: '正在上传网络数据...'
							})
							this._setSaveBatch(true, () => {
								
								uni.navigateTo({
									url: '/pages/record/realtime?id=' + this.id
								})
							})
							
						}
					}
				});
				
			},
			createTime() {

				this.interTime = setInterval(() => {

					this.newTime += 1

					this.startTime = this.formatSecond(this.newTime);

				}, 1000)
			},
			_setSaveBatch(isEnd,call) {
				// console.log('-----------------保存计分------')
				let sourceList = uni.getStorageSync('sourceList') || [];
				setSaveBatch(sourceList).then(res => {
					// console.log('-----------------保存计分success------')
					uni.hideLoading();
					if (isEnd) {
						console.log('cearTinmeot', this.timeServer);
						clearTimeout(this.timeServer);
						call();
						return;
					} else {
						this.setServerData()
					}
					
				}).catch(err => {
					console.log('-----------------保存计分error------')
					this.setServerData()
				});
			},
			setServerData() {
				clearTimeout(this.timeServer);
				this.timeServer = setTimeout(() => {
					console.log(1);
					//this.setServerData()
					this._setSaveBatch();
					// console.log('1-----time', sourceList)					
				}, 3000)
			},
			_getlistAll() {
				getlistAll({tournamentId: this.id}).then(res => {
					
					if (res.list.length == 0) {
						uni.removeStorageSync('sourceList');
						uni.removeStorageSync('sourceTwo');
						uni.removeStorageSync('sourceOne');
						uni.removeStorageSync('fanGuiNumOne');
						uni.removeStorageSync('fanGuiNumTwo');
						uni.removeStorageSync('zanTingOne');
						uni.removeStorageSync('zanTingTwo');
						uni.removeStorageSync('fenData');
						uni.removeStorageSync('localId');
						return;
					}
					
					let info = uni.getStorageSync('info')
					
					let onedata = res.score.find(item => {
						return item.teamId == info.oneTeam.id
					})
					let twodata = res.score.find(item => {
						return item.teamId == info.twoTeam.id
					})
					
					uni.setStorageSync('sourceList', res.list)
					uni.setStorageSync('sourceOne',onedata.teamSubsectionScoreSumTotal)
					uni.setStorageSync('sourceTwo',twodata.teamSubsectionScoreSumTotal)
					this.oneSource = onedata.teamSubsectionScoreSumTotal;
					this.twoSource = twodata.teamSubsectionScoreSumTotal;
					uni.setStorageSync('fanGuiNumOne', onedata.countFoulNum)
					uni.setStorageSync('fanGuiNumTwo', twodata.countFoulNum)
					this.fanGuiNumOne = onedata.countFoulNum
					this.fanGuiNumTwo = twodata.countFoulNum
					uni.setStorageSync('zanTingOne', onedata.countStopNum)
					uni.setStorageSync('zanTingTwo', twodata.countStopNum)
					
					this.zanTingOne = onedata.countStopNum
					this.zanTingTwo = twodata.countStopNum
				})
			}
 		},
		onShow() {
			this.getFenNum();
			if (this.timeServer) {
				clearTimeout(this.timeServer);
			}
			this.setServerData();
			if (uni.getStorageSync('isRefresh')) {
				uni.setStorageSync('isRefresh', false)
				this._getlistAll();
			}
		},
		onLoad({
			id
		}) {
			this.id = id
			this.scoringWay = uni.getStorageSync('scoringWay')
			// 获取分数
			this._getlistAll();
			
			
			// 获取阵位置
			this.getCteateList()
			// this.getFenNum()
			this.createTime()
			getdownBox().then(res => {
				let downbox = [],
					otherdownbox = []

				res.data.forEach(item => {
					console.log(item.code)
					if (item.code != 5 && item.code != 6 && item.code != 7 && item.code != 16) {
						if (item.code == 10 || item.code == 11) {
							downbox.push({
								...item,
								bgColor: 'red'
							});
						} else {
							downbox.push({
								...item,
								bgColor: '#547B44'
							});
						}

					} else {
						otherdownbox.push({
							...item,
							bgColor: '#f6c605'
						})
					}
				})

				downbox.push({
					code: 0,
					info: '不中',
					bgColor: '#f6c605'
				})
				console.log(downbox, '----------')
				this.downbox = downbox;
				this.otherdownbox = otherdownbox;
				console.log(this.downbox)
			});
			let info = uni.getStorageSync('info')

			this.teamOneColour = info.teamOneColour
			this.teamTwoColour = info.teamTwoColour
		}
	}
</script>

<style scoped lang="scss">
	.item {
		width: 100rpx;
		height: 100rpx;
		background-color: red;
		position: absolute;
		border-radius: 50%;
		text-align: center;
		border: 1px solid #fff;
		padding: 10rpx;
		font-size: 35rpx;
		font-weight: 700;
		z-index: 0;
		pointer-events: bounding-box;

		view {
			position: relative;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 100%;
			border-radius: 50%;
			box-shadow: inset 0px 0px 9px 5px rgba(255, 255, 255, 0.5), 7px 7px 20px 0px rgba(0, 0, 0, 0.1), 4px 4px 5px 0px rgba(0, 0, 0, 0.1);
		}

		&.cur {

			animation: selectKey .5s infinite;
		}

		.name {
			margin-top: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			font-size: 22rpx;
			padding: 5rpx 0;
			background-color: #333;
		}

		.num {
			display: flex;
			width: 30rpx;
			height: 30rpx;
			align-items: center;
			justify-content: center;
			position: absolute;
			font-size: 22rpx;
			right: 0;
			top: -11px;
			padding: 5rpx;
			border-radius: 30rpx;
			background-color: #333;
		}
	}

	.contentItem {
		position: relative;
		.item {
			color: #fff;
		}
	}

	.content {
		width: 100%;
		display: flex;
		position: relative;
		margin-bottom: 40rpx;

		.add-1 {
			position: absolute;
			width: 100rpx;
			height: 100rpx;
			top: 330rpx;
			left: 240rpx;
			z-index: 99;
		}
		.add-2 {
			position: absolute;
			width: 100rpx;
			height: 100rpx;
			top: 330rpx;
			right: 30rpx;
			z-index: 99;
		}

		.line1 {
			position: absolute;
			width: 1px;
			height: 100%;
			border: 1px dashed #333;
			right: calc(50% - 8px);
		}

		.line2 {
			position: absolute;
			width: 100%;
			height: 1px;
			border: 1px dashed #333;
			top: 320rpx;
			//right: calc(50% + 8px);
		}

		.line3 {
			position: absolute;
			width: 100%;
			height: 1px;
			border: 1px dashed #333;
			top: 480rpx;
			//right: calc(50% + 8px);
		}

		.ti1 {
			position: absolute;
			font-size: 24rpx;
			top: 500rpx;
			left: 13%;
		}

		.ti2 {
			position: absolute;
			font-size: 24rpx;
			top: 500rpx;
			right: 19%;
		}

		.contentItem {
			width: 50%;
			// height: 70vh;
		}

		.horizontal {
			height: 100%;
		}
	}

	.downbox {
		display: flex;
		flex-wrap: wrap;
		padding: 30rpx 20rpx;

		view {
			margin: 10rpx 0rpx 20rpx 16rpx;
			width: calc((100% / 3) - 20rpx);
			background-color: #60ABB1;
			padding: 40rpx 30rpx;
			text-align: center;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
		}
	}

	.downboxList {
		padding: 30rpx 20rpx;

		.downbox {
			padding: 0;
		}
	}

	.header {
		display: flex;
		position: relative;

		text {
			display: flex;
			position: absolute;
			left: 0;
			right: 0;
			margin: auto;
			width: 25%;
			justify-content: center;
			background-color: #fff;
			padding: 20rpx;
			top: 10rpx;
			box-shadow: 0 0px 1px 0 #5d5d5d;
			font-size: 28rpx;
			font-weight: 800;
		}

		view {
			width: 50%;
			padding: 30rpx;

			&:nth-child(3) {
				text-align: right;
				background-color: #333333;
				color: #fff;
			}
		}
	}

	.headerTwo {
		display: flex;
		width: 100%;
		justify-content: space-between;
		padding: 0 20rpx 30rpx 20rpx;
		margin-bottom: 20rpx;
		height: 140rpx;

		.oneItem {
			width: 30%;
			display: flex;
			flex-direction: row;
			align-items: center;
			font-size: 26rpx;
			justify-content: space-between;
			padding-right: 10rpx;
		}

		.twoItem {
			width: 30%;
			display: flex;
			flex-direction: row;
			align-items: center;
			font-size: 26rpx;
			justify-content: space-between;
			padding-left: 10rpx;
		}

		.cent {
			display: flex;
			text-align: center;
			justify-content: space-between;
			width: 40%;
			background-color: #333;
			align-items: center;
			color: #fff;
		}
	}

	.btns {
		display: flex;

		button {
			font-size: 28rpx;

			&:nth-child(1) {
				background-color: #60ABB1;
				color: #fff;
			}

			&:nth-child(2) {
				background-color: #E56255;
				color: #fff;
			}

			&:nth-child(3) {
				background-color: #333333;
				color: #fff;
			}
		}
	}

	@keyframes selectKey {
		0% {
			background-color: #fff;
			color: #333
		}

		100% {
			background: linear-gradient(to top, #000000, #c6946d, red, #CDDC39);
			color: #fff;
		}
	}
</style>